package config

import (
	"github.com/gin-gonic/gin"
	"my-blog/main/controller"
)

func InitGinConfig(router *gin.Engine) *gin.Engine {
	// 配置前端模板
	router.LoadHTMLGlob("resources/templates/**")
	// 配置静态资源
	router.Static("/assets", "resources/static")

	// 添加 controller
	webController(router)
	errorController(router)
	articleController(router)

	return router
}

// controller 处理------------------

func webController(router *gin.Engine) {
	router.GET("/", func(c *gin.Context) {
		controller.HomeController(c)
	})

	router.GET("/about", func(c *gin.Context) {
		controller.AboutController(c)
	})

	router.GET("/album", func(c *gin.Context) {
		controller.AlbumController(c)
	})

	router.GET("/whisper", func(context *gin.Context) {
		controller.WhisperController(context)
	})

	router.GET("/message", func(context *gin.Context) {
		controller.MessageController(context)
	})

	router.GET("/details", func(context *gin.Context) {
		controller.DetailsController(context)
	})
}

func errorController(router *gin.Engine) {
	router.NoRoute(HandlerFunc)
}

func HandlerFunc(context *gin.Context) {
	controller.Error404(context)
}

func articleController(router *gin.Engine) {
	router.GET("/article/details/:id", func(context *gin.Context) {
		controller.ArticleDetailsController(context)
	})
}
